/*
 * Author: Christopher Rondot
 * Contact: codiplex@gmail.com
 * Date: August 8, 2009
 * Comment: GTUG Campout Meeting
 */

package com.google.wavesurferrobot;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;
import com.google.wavesurferrobot.data.FetchWaveData;
import com.google.wavesurferrobot.data.StoreWaveData;
import com.google.wavesurferrobot.data.objects.BlipData;
import com.google.wavesurferrobot.data.objects.WaveData;


/* All available 
* WAVELET_BLIP_CREATED
* WAVELET_BLIP_REMOVED
* WAVELET_PARTICIPANTS_CHANGED
* WAVELET_TIMESTAMP_CHANGED
* WAVELET_TITLE_CHANGED
* WAVELET_VERSION_CHANGED
* BLIP_CONTRIBUTORS_CHANGED
* BLIP_DELETED
* BLIP_SUBMITTED
* BLIP_TIMESTAMP_CHANGED
* BLIP_VERSION_CHANGED
* DOCUMENT_CHANGED
* FORM_BUTTON_CLICKED 
* */

@SuppressWarnings("serial")
public class WavesurferrobotServlet  extends AbstractRobotServlet {

	  @Override
	  public void processEvents(RobotMessageBundle bundle) {
	    Wavelet wavelet = bundle.getWavelet();
	    
	    // The robot announces its presence and stores the wave
	    if (bundle.wasSelfAdded()) {
	      Blip blip = wavelet.appendBlip();
	      TextView textView = blip.getDocument();
	      textView.append("I'm alive!");
	      BlipData dataBlip = new BlipData(blip.getBlipId(), new Date(), Long.valueOf(wavelet.getVersion()), textView.getText());
	      WaveData dataWave = new WaveData(wavelet.getCreator(), wavelet.getParticipants(), wavelet.getWaveId(), new java.util.Date(), Long.valueOf(wavelet.getVersion()));
	    	/*
	    	 * THIS IS TOTALLY MESSED UP, WHEN YOU HAVE A JDO ONE TO MANY RELATIONSHIP,
	    	 * YOU HAVE TO HAVE AT LEAST 1 OBJECT IN A LIST<TYPE> MEMBER FOR IT TO STORE PROPERLY
	    	 * WHEN YOU COMMIT THE TRANSACTION
	    	 * 
	    	 * OTHERWISE IT THROWS SOME TYPE OF INDEX MISSING ERROR. THIS IS AN API BUG
	    	 * A VERY, VERY, VERY, VERY, VERY NASTY AND UNDOCUMENTED ONE
	    	 */
	      dataWave.addBlip(dataBlip);
	      List<WaveData> dwList = FetchWaveData.fetchDataWavesFromAuthor(wavelet.getCreator());
	      textView.append("\r\n i am not dead blame datastore!: ");
	      try {
	        String author = dwList.get(0).getAuthor();
	        textView.append("\r\nDATABASE FETCH: AUTHOR IS: " + author + " BLIPS: " + dwList.get(0).getDataBlipSets().size());
	      } catch (Exception excep) {
	        textView.append("\r\nERROR: THE JDO INSERT FAILED!!!: Exception: " + excep.getMessage());
	      }
	    }
	            
	    for (Event e: bundle.getEvents()) {
	      // Change the list of participants, greet the new one, wave the ones that left
	      if (e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED) {    
	        Blip blip = wavelet.appendBlip();
            Collection<String> addedParticipants = (Collection<String>) e.getAddedParticipants();
	        StringBuffer sb = new StringBuffer();
	        Iterator it = addedParticipants.iterator();
            while (it.hasNext()) {
               sb.append((String) it.next());
            }
	        TextView textView = blip.getDocument();
	        wavelet.getRootBlip().getChildren();
	        textView.append("Hello to " + sb); // forget that
	      }
	      
	      //  WAVELET_BLIP_CREATED add the blip to the DataWave
	      if (e.getType() == EventType.WAVELET_BLIP_CREATED) { 
	    	Blip blip = e.getBlip();
	    	TextView textView = blip.getDocument();
	    	BlipData dataBlip = new BlipData(blip.getBlipId(), new Date(blip.getLastModifiedTime()), blip.getVersion(), blip.getDocument().getText());
	    	String wave_id = blip.getWavelet().getWaveId();
	    	textView.append("wave_id = "+wave_id);	
	    	WaveData dataWave = FetchWaveData.fetchDataWaveFromGoogleWaveID(wave_id);
	    	dataWave.addBlip(dataBlip);
	        
	        textView.append("i think i added smth at the datastore"); // forget that
	      }
	      
		  //  WAVELET_BLIP_REMOVED remove the blip from the DataWave
	      if (e.getType() == EventType.WAVELET_BLIP_REMOVED) { 
	    	Blip blip = e.getBlip();
	    	String wave_id = blip.getWavelet().getWaveId();
	    	BlipData dataBlip = new BlipData(blip.getBlipId(), new Date(blip.getLastModifiedTime()), blip.getVersion(), blip.getDocument().getText());
	    	WaveData dataWave = FetchWaveData.fetchDataWaveFromGoogleWaveID(wave_id);
	    	dataWave.removeBlip(dataBlip);
	      }
	      
		  //  BLIP_VERSION_CHANGED update the blip in the DataWave	      
	      if (e.getType() == EventType.BLIP_VERSION_CHANGED) { 
	    	Blip blip = e.getBlip();
	    	String wave_id = blip.getWavelet().getWaveId();
	    	BlipData dataBlip = new BlipData(blip.getBlipId(), new Date(blip.getLastModifiedTime()), blip.getVersion(), blip.getDocument().getText());
	    	WaveData dataWave = FetchWaveData.fetchDataWaveFromGoogleWaveID(wave_id);
	    	dataWave.addBlip(dataBlip);
          }
	    }          

	  } 
}
// hi i am rushin